{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\" Convolutional network applied to CIFAR-10 dataset classification task.\n",
    "References:\n",
    "    Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.\n",
    "Links:\n",
    "    [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)\n",
    "\"\"\"\n",
    "from __future__ import division, print_function, absolute_import\n",
    "import tflearn\n",
    "from tflearn.data_utils import shuffle, to_categorical\n",
    "from tflearn.layers.core import input_data, dropout, fully_connected\n",
    "from tflearn.layers.conv import conv_2d, max_pool_2d\n",
    "from tflearn.layers.estimator import regression\n",
    "from tflearn.data_preprocessing import ImagePreprocessing\n",
    "from tflearn.data_augmentation import ImageAugmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Data loading and preprocessing\n",
    "from tflearn.datasets import cifar10\n",
    "(X, Y), (X_test, Y_test) = cifar10.load_data()\n",
    "X, Y = shuffle(X, Y)\n",
    "Y = to_categorical(Y, 10)\n",
    "Y_test = to_categorical(Y_test, 10)\n",
    "\n",
    "# Real-time data preprocessing\n",
    "img_prep = ImagePreprocessing()\n",
    "img_prep.add_featurewise_zero_center()\n",
    "img_prep.add_featurewise_stdnorm()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Real-time data augmentation\n",
    "img_aug = ImageAugmentation()\n",
    "img_aug.add_random_flip_leftright()\n",
    "img_aug.add_random_rotation(max_angle=25.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Convolutional network building\n",
    "network = input_data(shape=[None, 32, 32, 3],\n",
    "                     data_preprocessing=img_prep,\n",
    "                     data_augmentation=img_aug)\n",
    "network = conv_2d(network, 32, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2)\n",
    "network = conv_2d(network, 64, 3, activation='relu')\n",
    "network = conv_2d(network, 64, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2)\n",
    "network = fully_connected(network, 512, activation='relu')\n",
    "network = dropout(network, 0.5)\n",
    "network = fully_connected(network, 10, activation='softmax')\n",
    "network = regression(network, optimizer='adam',\n",
    "                     loss='categorical_crossentropy',\n",
    "                     learning_rate=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Step: 26049  | total loss: \u001b[1m\u001b[32m0.32852\u001b[0m\u001b[0m | time: 119.623s\n",
      "| Adam | epoch: 050 | loss: 0.32852 - acc: 0.8853 -- iter: 49920/50000\n",
      "Training Step: 26050  | total loss: \u001b[1m\u001b[32m0.32454\u001b[0m\u001b[0m | time: 127.685s\n",
      "| Adam | epoch: 050 | loss: 0.32454 - acc: 0.8853 | val_loss: 0.64020 - val_acc: 0.8192 -- iter: 50000/50000\n",
      "--\n"
     ]
    }
   ],
   "source": [
    "# Train using classifier\n",
    "model = tflearn.DNN(network, tensorboard_verbose=0)\n",
    "model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),\n",
    "          show_metric=True, batch_size=96, run_id='cifar10_cnn')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
